﻿namespace AddressBook.Business.Person
{
  #region "Using Statements"
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;

  using Csla;
  using Csla.Data;
  #endregion

  [Serializable]
  public class AddressInfo : ReadOnlyBase<AddressInfo>
  {
    #region "Business Methods"

    #region "Private Static Member Variables"

    private static PropertyInfo<Int32> IdProperty = RegisterProperty<Int32>(a => a.Id);
    private static PropertyInfo<String> Address1Property = RegisterProperty<String>(a => a.Address1);
    private static PropertyInfo<String> Address2Property = RegisterProperty<String>(a => a.Address2);
    private static PropertyInfo<String> CityProperty = RegisterProperty<String>(a => a.City);
    private static PropertyInfo<Int32> StateProvinceIdProperty = RegisterProperty<Int32>(a => a.StateProvinceId);
    private static PropertyInfo<String> PostalCodeProperty = RegisterProperty<String>(a => a.PostalCode);

    #endregion

    #region "Public Properties"

    public Int32 Id
    {
      get { return GetProperty(IdProperty); }
      internal set { LoadProperty(IdProperty, value); }
    }

    public String Address1
    {
      get { return GetProperty(Address1Property); }
      internal set { LoadProperty(Address1Property, value); }
    }

    public String Address2
    {
      get { return GetProperty(Address2Property); }
      internal set { LoadProperty(Address2Property, value); }
    }

    public String City
    {
      get { return GetProperty(CityProperty); }
      internal set { LoadProperty(CityProperty, value); }
    }

    public Int32 StateProvinceId
    {
      get { return GetProperty(StateProvinceIdProperty); }
      internal set { LoadProperty(StateProvinceIdProperty, value); }
    }

    public String PostalCode
    {
      get { return GetProperty(PostalCodeProperty); }
      internal set { LoadProperty(PostalCodeProperty, value); }
    }

    public String FullAddress
    {
      get
      {
        using (var ctx = ContextManager<AddressBook.Business.Model.AddressBookDataContext>.GetManager(AddressBook.Business.Model.Database.AddressBook))
        {
          AddressBook.Business.Model.StateProvince stateProvince = ctx.DataContext.StateProvinces.First(sp => sp.Id == this.StateProvinceId);

          if (String.IsNullOrEmpty(this.Address2))
          {
            return String.Format("{0} {1}, {2} {3}", this.Address1, this.City, stateProvince.StateProvinceCode, this.PostalCode);
          }

          return String.Format("{0} {1} {2}, {3} {4}", this.Address1, this.Address2, this.City, stateProvince.StateProvinceCode, this.PostalCode);
        }
      }
    }

    #endregion

    #endregion

    #region "Constructors"

    internal AddressInfo(Int32 id, String address1, String address2, String city, Int32 stateProvinceId, String postalCode)
    {
      this.Id = id;
      this.Address1 = address1;
      this.Address2 = address2;
      this.City = city;
      this.StateProvinceId = stateProvinceId;
      this.PostalCode = postalCode;
    }

    #endregion
  }
}
